package flc.ast.fragment;

import android.Manifest;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.FileManageActivity;
import flc.ast.activity.FileScanQrActivity;
import flc.ast.activity.FileTransferActivity;
import flc.ast.adapter.TransferRecordAdapter;
import flc.ast.bean.TransferRecordBean;
import flc.ast.databinding.FragmentTransferBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TransferFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TransferFragment extends BaseNoModelFragment<FragmentTransferBinding> {

    private List<TransferRecordBean> listShow = new ArrayList<>();
    private TransferRecordAdapter recordAdapter;
    private int type;


    @Override
    protected int onCreate() {
        return R.layout.fragment_transfer;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivFileReceive.setOnClickListener(this);
        mDataBinding.ivFileTransfer.setOnClickListener(this);
        mDataBinding.ivTransfer.setOnClickListener(this);
        mDataBinding.ivFileCompress.setOnClickListener(this);
        mDataBinding.tvFileTransferRecord.setOnClickListener(this);
        mDataBinding.tvFileReceiveRecord.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        recordAdapter = new TransferRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivTransferRecordItemDelete);
        recordAdapter.setOnItemChildClickListener(this);
        recordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivFileTransfer://文件传输
            case R.id.ivTransfer:
                getTransferPermission();
                break;
            case R.id.ivFileReceive://文件接收
                getReceivePermission();
                break;
            case R.id.ivFileCompress://文件压缩
                startActivity(FileManageActivity.class);
                break;
            case R.id.tvFileTransferRecord://传输记录
                clearSelection();
                type = 0;
                mDataBinding.tvFileTransferRecord.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvFileTransferRecord.setBackgroundColor(Color.parseColor("#6872E1"));
                getTransferData();
                break;
            case R.id.tvFileReceiveRecord://接收记录
                clearSelection();
                type = 1;
                mDataBinding.tvFileReceiveRecord.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvFileReceiveRecord.setBackgroundColor(Color.parseColor("#6872E1"));
                getReceiveData();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivTransferRecordItemDelete://删除
                listShow.remove(position);
                if (type == 0) {
                    AppSpUtils.saveTransferRecordList(listShow);
                    getTransferData();
                } else {
                    AppSpUtils.saveReceiveRecordList(listShow);
                    getReceiveData();
                }
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (type == 0) {
            getTransferData();
        } else {
            getReceiveData();
        }
    }

    private void getTransferData() {
        listShow.clear();
        List<TransferRecordBean> listSp = AppSpUtils.getTransferRecordList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getReceiveData() {
        listShow.clear();
        List<TransferRecordBean> listSp = AppSpUtils.getReceiveRecordList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getTransferPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_ALL_MEDIA, Manifest.permission.READ_CONTACTS)
                .reqPermissionDesc(getString(R.string.get_transfer_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(FileTransferActivity.class);
                    }
                }).request();
    }

    private void getReceivePermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_ALL_MEDIA, Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.CAMERA)
                .reqPermissionDesc(getString(R.string.get_receive_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(FileScanQrActivity.class);
                    }
                }).request();
    }

    private void clearSelection() {
        mDataBinding.tvFileTransferRecord.setTextColor(Color.parseColor("#858BD2"));
        mDataBinding.tvFileTransferRecord.setBackground(null);
        mDataBinding.tvFileReceiveRecord.setTextColor(Color.parseColor("#858BD2"));
        mDataBinding.tvFileReceiveRecord.setBackground(null);
    }

}